﻿/* 
 * Copyright 2010 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ngilead.Common;
using System.Runtime.Serialization;

namespace Ngilead.Common.Base
{
    /// <summary>
    /// LightEntity class
    /// </summary>
    [DataContract(IsReference = true)]
    [KnownType(typeof(SerializableId))]
    [KnownType(typeof(List<SerializableId>))]
    public class LightEntity : ILightEntity
    {
        #region Protected properties

        /// <summary>
        /// Map of persistence proxy informations.
        /// The key is the property name, the value is a map with
        /// persistence informations filled by the persistence util
        /// implementation
        /// </summary>
        [DataMember]
        protected internal virtual Dictionary<string, Dictionary<string, Object>> _proxyInformations { get; set; }


        /// <summary>
        /// Map of persistence initialisation state.
        /// The key is the property name, the value is the initialization state
        /// of the property
        /// </summary>
        [DataMember]
        protected internal virtual Dictionary<string, bool> _initializationMap { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public LightEntity()
        {
        }

        #endregion

        #region ILightEntity Membres

        /// <summary>
        /// Add proxy information for the argument property.
        /// </summary>
        /// <param name="property">the property name</param>
        /// <param name="proxyInfo">map of proxy informations (string serialized form)</param>
        public virtual void AddProxyInformation(string property,
                                        object proxyInfo)
        {
            if (_proxyInformations == null)
            {
                _proxyInformations = new Dictionary<string, Dictionary<string, Object>>();
            }
            Dictionary<String, Object> temp = proxyInfo as Dictionary<String, Object>;
            if (temp != null)
            {
                _proxyInformations[property] = temp;
            }
            else
            {
                throw new ArgumentException("proxyInfo is not type of Dictionary<String, Object>");
            }
        }

        /// <summary>
        /// Remove a property proxy information
        /// </summary>
        /// <param name="property">the property name</param>
        public virtual void RemoveProxyInformation(string property)
        {
            if (_proxyInformations != null)
            {
                _proxyInformations.Remove(property);
            }
        }

        /// <summary>
        /// Get proxy information for the argument property
        /// </summary>
        /// <param name="property">the property name</param>
        /// <returns>the proxy informations for the property _ string serialized form(can be null)</returns>
        public virtual object GetProxyInformation(string property)
        {
            if ((_proxyInformations != null) &&
                (_proxyInformations.ContainsKey(property)))
            {
                return _proxyInformations[property];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Debug method : write the declared proxy information
        /// </summary>
        /// <returns>a human readable description of proxy information</returns>
        public virtual String GetDebugString()
        {
            if (_proxyInformations != null)
            {
                return _proxyInformations.ToString();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Is the property initialized
        /// </summary>
        /// <param name="property">the property name</param>
        /// <returns>true if the property was initialised, false otherwise</returns>
        public virtual bool IsInitialized(string property)
        {
            if (_initializationMap == null)
            {
                return true;
            }

            if (_initializationMap.Keys.Contains(property))
            {
                return _initializationMap[property];
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Set the initialization state of a property.
        /// </summary>
        /// <param name="property">the property name</param>
        /// <param name="initialized">initialized</param>
        public virtual void SetInitialized(string property, bool initialized)
        {
            if (_initializationMap == null)
            {
                _initializationMap = new Dictionary<string, bool>();
            }
            _initializationMap[property] = initialized;
        }

        /// <summary>
        /// Check if the entity has proxy inforamtions
        /// </summary>
        /// <returns>true/false</returns>
        public virtual bool HasProxyInformations()
        {
            return ((_initializationMap != null && _initializationMap.Count > 0) ||
                     (_proxyInformations != null && _proxyInformations.Count > 0));
        }

        #endregion
    }
}
